<br />
<div class="titleStyle1">Request waiting for approval</div>
<?php if ($requests == null){ ?>
<div class="information2">no new requests</div>

<?php }else {?>
<script>
$(function () {
    $("#chkAll").click(function () {
        if ($("#chkAll").is(':checked')) {
            $(".request").prop("checked", true);
        } else {
            $(".request").prop("checked", false);
        }
    });
    
    $("#requestTable .request").click(function () {
        if ($("#chkAll").is(':checked')) {
            $("#chkAll").prop("checked", false);
        }
    });

    $('#requestTable .approvedButton').click(function(){
    	var request_id = $(this).parent().siblings('.id').text();
    	var cell = $(this).parent();
    	cell.html('<?php echo img(array('src'=>asset_url().'images/load.gif', 'alt'=> 'home')); ?>');
		var jqxhr = $.post( "request/single_approve", {request_id: request_id})
		.done(function(data) {
			if (data == '1'){
				cell.html('<label style="color:#00a10b;">Success</label>');
				var chk = cell.siblings('.chk').children();
				chk.attr("disabled", true);
				chk.prop("checked", false);
				chk.removeClass('request');
			}else
				cell.html('<label style="color:red;">fail</label>');
		})
		.fail(function() {
			cell.html('<label style="color:red;">fail</label>');
		});

    });
});
</script>
<div style="margin-top: 15px;">
	<span style="padding: 0px 10px; display: inline; margin: 0px 10px;"
		class="allowed"></span><label> valid</label> &nbsp;
		<span
		style="padding: 0px 10px; display: inline; margin: 0px 10px 10px 40px;"
		class="disallowed"></span>&nbsp;<label> not enough stock</label>
		&nbsp;
		<span style="padding: 0px 10px; display: inline; margin: 0px 10px 10px 40px;"
		class="inactive"></span>&nbsp;<label>item is inactive</label>
</div>
<br />
<div class="requestTable">
	<?php echo form_open('request/approve'); ?>
	<table id="requestTable">
		<tr>
			<td><input type="checkbox" id="chkAll" /></td>
			<td>ID</td>
			<td>Item</td>
			<td>Barcode</td>
			<td>Request Amount</td>
			<td>Approve</td>
		</tr>
		<?php 

		foreach ($requests as $request){
			echo '<tr class="';
			if ($request->status == Constants::item_status_normal){
				echo 'allowed';
			} else if ($request->status == Constants::item_status_inactive){
				echo 'inactive';
			} else
				echo 'disallowed';
			echo '" >';

			echo '<td class="chk">';
			echo '<input type="checkbox" name="chk[]" value="'. $request->request_id .'"';
			if ($request->status != Constants::item_status_normal){
				echo 'disabled';
			}else {
				echo 'class="request"';
			}
			echo ' />';
			echo '</td>';

			echo '<td class="id">';
			echo $request->request_id;
			echo '</td>';

			echo '<td>';
			echo $request->item_name;
			echo '</td>';

			echo '<td>';
			echo $request->item_barcode;
			echo '</td>';

			echo '<td>';
			echo $request->request_amount;
			echo '</td>';

			echo '<td style="text-align:center;">';
			echo '<input type="button" value ="Approve" name="btn" ';
			if ($request->status != 1){
				echo 'disabled class="disableButton"';
			}else {
				echo 'class="approvedButton"';
			}
			echo ' />';
			echo '</td>';

			echo '</tr>';
		}
		?>
	</table>
	<br /> <input type="submit" value="Approve selected requests"
		class="buttonStyle1" />
	<?php echo form_close(); ?>
</div>
<?php }?>
<br />
<hr />
<br />
<div class="titleStyle1">Approved items waiting to be received</div>
<?php if ($receives == null){ ?>
<div class="information2">All items are received</div>
<?php }else {?>
<div class="waitingTable">
	<table>
		<tr>
			<td>Request ID</td>
			<td>Branch ID</td>
			<td>Branch</td>
			<td>Branch's Location</td>
			<td>Tel</td>
			<td>Item</td>
			<td>Barcode</td>
			<td>Request Amount</td>
			<td>Approved Date</td>
		</tr>
		<?php 

		foreach ($receives as $receive){
		echo '<tr>';

		echo '<td>';
		echo $receive->request_id;
		echo '</td>';
		
		echo '<td>';
		echo $receive->branch_id;
		echo '</td>';
		
		echo '<td>';
		echo $receive->branch_name;
		echo '</td>';
		
		echo '<td>';
		echo $receive->branch_location;
		echo '</td>';
		
		echo '<td>';
		echo $receive->branch_tel;
		echo '</td>';

		echo '<td>';
		echo $receive->item_name;
		echo '</td>';

		echo '<td>';
		echo $receive->item_barcode;
		echo '</td>';

		echo '<td>';
		echo $receive->request_amount;
		echo '</td>';

		echo '<td>';
		echo $receive->request_approved_date;
		echo '</td>';

		echo '</tr>';
	}
	?>
	</table>
</div>
<?php }?>